package com.coder.oj.system.controller;

import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Answer;
import com.coder.oj.common.mapper.AnswerMapper;
import com.coder.oj.common.validate.ValidationGroups;
import com.coder.oj.system.dto.AnswerDto;
import com.coder.oj.system.mapstruct.AnswerMapstruct;
import com.coder.oj.system.query.AnswerQuery;
import com.coder.oj.system.service.AnswerService;
import com.coder.oj.system.utils.CheckUserUtils;
import com.coder.oj.system.vo.AnswerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = {"题解管理"})
@RestController
@RequestMapping("/answer")
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private AnswerMapstruct answerMapstruct;

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<AnswerVo>> getAnswerPage(@RequestBody AnswerQuery params) {
        return R.ok().setResult(answerService.getAnswerPage(params));
    }

    @GetMapping
    @ApiOperation("题解详情")
    public R<AnswerVo> getAnswerById(@RequestParam("id") Long id) {
        return R.ok().setResult(answerMapstruct.toVO(answerMapper.selectById(id)));
    }

    @JwtToken
    @PostMapping
    @ApiOperation("新增题解")
    public R saveAnswer(@Validated(ValidationGroups.Insert.class) @RequestBody AnswerDto params) {
        answerMapper.insert(answerMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @PutMapping
    @ApiOperation("修改题解")
    public R updateAnswer(@Validated(ValidationGroups.Update.class) @RequestBody AnswerDto params) {
        answerService.updateAnswer(params);
        return R.ok();
    }

    @JwtToken
    @DeleteMapping
    @ApiOperation("删除题解")
    @Transactional(rollbackFor = Exception.class)
    public R deleteAnswerById(@RequestParam("id") Long id) {
        answerService.deleteAnswerById(id);
        return R.ok();
    }
}
